package cz.muni.fi.pa165.soap.client.editPannel;

import cz.muni.fi.pa165.soap.CustomerDTO;
import cz.muni.fi.pa165.soap.func.Funcs;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author shifty
 */
public class EditCustomer extends JPanel {
   
    private ResourceBundle rb;
    private CustomerDTO customer;
    
    public EditCustomer(ResourceBundle rb, CustomerDTO cus) {
        this.rb=rb;
        this.customer=cus;
        init();
    }
    
    
    
    private void init() {
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        setToolTipText(rb.getString("main.customer")+" - "+( (customer!=null)?customer.getName():rb.getString("form.new") ));
        
        nameLabel = new JLabel(rb.getString("table.name"));
        addressLabel = new JLabel(rb.getString("table.address"));
        cityLabel = new JLabel(rb.getString("table.city"));
        zipLabel = new JLabel(rb.getString("table.zip"));
        countryLabel = new JLabel(rb.getString("table.country"));
        accountNumberLabel = new JLabel(rb.getString("table.accountNumber"));
        saveButton = new JButton(rb.getString("form.save"));
        
        Box name = Box.createHorizontalBox();
        name.add(nameLabel);
        name.add(nameField);
        Funcs.setFixedSize(nameLabel, defaultSize);
        Funcs.setFixedSize(nameField, defaultSize);
        Box street = Box.createHorizontalBox();
        street.add(addressLabel);
        street.add(addressField);
        Funcs.setFixedSize(addressLabel, defaultSize);
        Funcs.setFixedSize(addressField, defaultSize);
        Box zip = Box.createHorizontalBox();
        zip.add(zipLabel);
        zip.add(zipField);
        Funcs.setFixedSize(zipLabel, defaultSize);
        Funcs.setFixedSize(zipField, defaultSize);
        Box city = Box.createHorizontalBox();
        city.add(cityLabel);
        city.add(cityField);
        Funcs.setFixedSize(cityLabel, defaultSize);
        Funcs.setFixedSize(cityField, defaultSize);
        Box country = Box.createHorizontalBox();
        country.add(countryLabel);
        country.add(countryField);
        Funcs.setFixedSize(countryLabel, defaultSize);
        Funcs.setFixedSize(countryField, defaultSize);
        Box accountNumber = Box.createHorizontalBox();
        accountNumber.add(accountNumberLabel);
        accountNumber.add(accountNumberField);
        Funcs.setFixedSize(accountNumberLabel, defaultSize);
        Funcs.setFixedSize(accountNumberField, defaultSize);
        Box save = Box.createHorizontalBox();
        save.add(saveButton);
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    CustomerDTO c = getValues();
                    onSave.setObject(c);
                    onSave.execute();
                } catch(IllegalStateException ise) {
                    JOptionPane.showMessageDialog((JButton)e.getSource(),
                        rb.getString("form.warning.input.empty"),
                        rb.getString("form.warning"),
                        JOptionPane.WARNING_MESSAGE);
                }   
            }
        });
        add(name);
        add(street);
        add(zip);
        add(city);
        add(country);
        add(accountNumber);
        add(save);
        add(Box.createVerticalGlue());
        
        setValues();
    }
    private void setValues() {
        if(customer == null) {
            return;
        }
        nameField.setText(customer.getName());
        addressField.setText(customer.getAddress());
        zipField.setText(customer.getZip());
        cityField.setText(customer.getCity());
        countryField.setText(customer.getCountry());
        accountNumberField.setText(customer.getAccountNumber());
    }
    private CustomerDTO getValues() {
        String name = nameField.getText();
        String address = addressField.getText();
        String zip = zipField.getText();
        String city = cityField.getText();
        String country = countryField.getText();
        String accountNumber = accountNumberField.getText();
        
        if(name.isEmpty() || address.isEmpty() || zip.isEmpty() || city.isEmpty() ||
                country.isEmpty() || accountNumber.isEmpty()) {
            throw new IllegalStateException("some of inputs are not filled");
        }
        CustomerDTO c = new CustomerDTO();
        if(customer != null) {
            c.setId(customer.getId());
        }
        c.setName(name);
        c.setAddress(address);
        c.setZip(zip);
        c.setCity(city);
        c.setCountry(country);
        c.setAccountNumber(accountNumber);
        return c;
    }
    
    private Dimension defaultSize = new Dimension(150, 20);
    
    private JTextField nameField = new JTextField();
    private JTextField addressField = new JTextField();
    private JTextField zipField = new JTextField();
    private JTextField cityField = new JTextField();
    private JTextField countryField = new JTextField();
    private JTextField accountNumberField = new JTextField();
    
    private JLabel nameLabel;
    private JLabel addressLabel;
    private JLabel zipLabel;
    private JLabel cityLabel;
    private JLabel countryLabel;
    private JLabel accountNumberLabel;
    
    private JButton saveButton;
    
    private OnSave<CustomerDTO> onSave;
    
    public void onSave(OnSave<CustomerDTO> al) {
        onSave = al;
    }
}
